package com.yixun.qfbao.hander;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.PremisesApiService;
import com.yixun.qfbao.apidto.GetPremisesCityGroupListReqDto;
import com.yixun.qfbao.apidto.GetPremisesCityGroupListResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetPremisesCityGroupListReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author guifang.weng
 * @create 2017-12-01 14:07
 **/
@Service
public class GetPremisesCityGroupListHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetPremisesCityGroupListHander.class);

    @Autowired
    private PremisesApiService premisesApiService;
    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<GetPremisesCityGroupListResDto> res = new BaseResponseDTO<>();
        GetPremisesCityGroupListReqVo reqVo = (GetPremisesCityGroupListReqVo)reqDto;
        GetPremisesCityGroupListReqDto getPremisesCityGroupListReqDto = reqVo.getParams();
        if(StringUtils.isNotBlank(getPremisesCityGroupListReqDto.getUserId())){
        	   PartnerInfo pi = loginUtils.getUser(getPremisesCityGroupListReqDto.getUserId());
               if (pi == null) {
                   res.setResult(ResultEnum.FAIL.getNo());
                   res.setErrorMsg("未找到用户信息");
                   return res;
               }
            getPremisesCityGroupListReqDto.setMerchantId(pi.getHigherid());
        }
        try {
            GetPremisesCityGroupListResDto resDto = premisesApiService.getPremisesCityGroupListService(getPremisesCityGroupListReqDto);
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("GetPremisesCityGroupListHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("获取城市信息失败");
        }
        return res;
    }
}
